#include "ssicloud_news.h"

ssicloud::NewsTypeList::NewsTypeList()
    : ListObject<NewsTypeListLNode>(LQ_BASE_URL "/app/newsType/list"
                           , OBJECT_NAME_NewsTypeList
                           , NetUrl::NET_HTTP_METHOD_GET)
{
    mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);
}

bool ssicloud::NewsTypeList::requestAll()
{
    return obtain();
}

void ssicloud::NewsTypeList::profile()
{
    ListTable<NewsTypeListLNode>::iterator it = mNodes.begin();
    GEN_Printf(LOG_INFO, "---------获取资讯类型列表-------");

    for (; it != mNodes.end(); ++it)
    {
         GEN_Printf(LOG_DUMP, "ID:%s, 类型名称:%s, 备注:%s, 最后编辑时间:%s",
                    it->id.string(), it->name.c_str(),
                    it->remark.c_str(), it->updateTime.c_str());
    }
    GEN_Printf(LOG_INFO, "---------完成-------");
}

int ssicloud::NewsTypeList::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *dataJson = cJSON_GetObjectItem(root, "data");
    if (dataJson)
    {
        for (cJSON *item = dataJson->child; NULL != item; item = item->next )
        {
            NewsTypeListLNode tmp;

            JSON_VALUETRING_SWAP_BYTESTRING(item, id, tmp.id);
            JSON_VALUETRING_SWAP_BYTESTRING(item, name, tmp.name);
            JSON_VALUETRING_SWAP_BYTESTRING(item, remark, tmp.remark);
            JSON_VALUETRING_SWAP_BYTESTRING(item, updateTime, tmp.updateTime);

            // GEN_Printf(LOG_DEBUG, "items size=%d", tmp.items.size());
            mNodes.push_back(tmp);
        }

        if (mNodes.empty())
        {
            GEN_Printf(LOG_WARN, "load Category is empty.");
            ret = KL_DATA_PRISER_EMPTY;
        } else
        {
            profile();
        }
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}

ssicloud::NewsSelectPage::NewsSelectPage()
    : ListObject<NewsSelectPageLNode>(LQ_BASE_URL "/app/news/selectPage"
                           , OBJECT_NAME_NewsSelectPage
                           , NetUrl::NET_HTTP_METHOD_GET)
{
    mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);

    mUrl.append("status", "1");
}

bool ssicloud::NewsSelectPage::requestAll(const char *id, int pageNum, int pageSize)
{
    if (isLoading()) return true;

    m_newsTypeId = id;
    m_pageNum = SFString::number(pageNum);
    m_pageSize = SFString::number(pageSize);

    // mUrl.appendChange("newsTypeId", m_newsTypeId.c_str());
    mUrl.appendChange("pageNum", m_pageNum.c_str());
    mUrl.appendChange("pageSize", m_pageSize.c_str());

    return obtain();
}

void ssicloud::NewsSelectPage::profile()
{
    ListTable<NewsSelectPageLNode>::iterator it = mNodes.begin();
    GEN_Printf(LOG_INFO, "---------新闻资讯-分页查询-------");

    for (; it != mNodes.end(); ++it)
    {
          GEN_Printf(LOG_DUMP, "标题:%s, 内容:%s, 创建人:%s, 创建时间:%s, picPath: %s",
                     it->title.string(), it->content.c_str(),
                     it->operatr.c_str(), it->updateTime.c_str(), it->picPath.c_str());
    }
    GEN_Printf(LOG_INFO, "---------完成-------");
}

int ssicloud::NewsSelectPage::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *dataJson = cJSON_GetObjectItem(root, "data");
    if (dataJson)
    {
        cJSON *records = cJSON_GetObjectItem(dataJson, "records");
        if (records) {
            for (cJSON *item = records->child; NULL != item; item = item->next )
            {
                NewsSelectPageLNode tmp;

                JSON_VALUETRING_SWAP_BYTESTRING(item, title, tmp.title);
                JSON_VALUETRING_SWAP_BYTESTRING(item, content, tmp.content);
                JSON_VALUETRING_SWAP_BYTESTRING(item, picPath, tmp.picPath);
                JSON_VALUETRING_SWAP_BYTESTRING(item, isTop, tmp.isTop);
                JSON_VALUETRING_SWAP_BYTESTRING(item, topStartTime, tmp.topStartTime);
                JSON_VALUETRING_SWAP_BYTESTRING(item, topEndTime, tmp.topEndTime);
                JSON_VALUETRING_SWAP_BYTESTRING(item, operator, tmp.operatr);
                JSON_VALUETRING_SWAP_BYTESTRING(item, createTime, tmp.createTime);
                JSON_VALUETRING_SWAP_BYTESTRING(item, updateTime, tmp.updateTime);

                // GEN_Printf(LOG_DEBUG, "items size=%d", tmp.items.size());
                mNodes.push_back(tmp);
            }
        }

        if (mNodes.empty())
        {
            GEN_Printf(LOG_WARN, "load Category is empty.");
            ret = KL_DATA_PRISER_EMPTY;
        } else
        {
            profile();
        }
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}
